
const apiUrl = 'http://127.0.0.1:5000/records_question';

// Page({
//   data: {
//     questions: [], 
//     currentQuestionIndex: 0, 
//   },
//   onLoad: function () {
//     wx.request({
//       url: apiUrl,
//       success: (res) => {
//         const questionArr = res.data;
      
//         this.setData({
//           questions: questionArr,
//         });
//       },
//     });
//   },
//   showAnswer: function (e) {
//     const index = e.currentTarget.dataset.index;
//     const questions = this.data.questions;
//     questions[index].showAnswer = true;
    
//     this.setData({
//       questions: questions,
//     });
//   },
// });

Page({
    data: {
      questions: [], // 问题列表
      currentIndex: 0, // 当前显示的题目索引
      currentQuestion: 1, // 当前题目的序号
    },
  
    onLoad: function () {
      wx.request({
        url: apiUrl,
        success: (res) => {
          const questionArr = res.data;
          // 保存问题列表
          this.setData({
            questions: questionArr,
          });
        },
      });
    },
  
    // 查看答案
    showAnswer: function (e) {
      const index = e.currentTarget.dataset.index;
      const questions = this.data.questions;
      // 设置当前问题显示答案
      questions[index].showAnswer = true;
      // 更新问题列表
      this.setData({
        questions: questions,
      });
    },
  
    // 上一题
    prevQuestion: function () {
      let index = this.data.currentIndex;
      if (index > 0) {
        index--;
        // 更新当前显示的题目索引和题目序号
        this.setData({
          currentIndex: index,
          currentQuestion: index + 1,
        });
      }
    },
  
    // 下一题
    nextQuestion: function () {
      let index = this.data.currentIndex;
      if (index < this.data.questions.length - 1) {
        index++;
        // 更新当前显示的题目索引和题目序号
        this.setData({
          currentIndex: index,
          currentQuestion: index + 1,
        });
      }
    },
  });